package com.decheng.java8.lambda;

import com.decheng.java8.entity.Employee;
import org.junit.Test;

import java.util.Arrays;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * 一、构造器引用
 * 和方法引用类似，函数式接口的抽象方法的形参列表和构造器的形参列表一致。抽象方法的返回值类型即为构造器所属的类的类型
 * 二、数组引用
 * 可以把数组看做是一个特殊的类，则写法与构造器引用一致。
 */
public class ConstructorRefTest {
    // 构造器引用
    // Supplier 中的 T get()
    @Test
    public void test1() {
        Supplier<Employee> supplier = () -> new Employee();
        Employee employee = supplier.get();
        System.out.println("employee = " + employee);
        System.out.println("----------------------------------------✂----------------------------------------");
        Supplier<Employee> supplier1 = Employee::new;
        Employee employee1 = supplier1.get();
        System.out.println("employee1 = " + employee1);
    }

    // Function中的R apply(T t)
    @Test
    public void test2() {
        Function<Integer, Employee> function = id -> new Employee(id);
        Employee apply = function.apply(1);
        System.out.println("apply = " + apply);
        System.out.println("----------------------------------------✂----------------------------------------");
        Function<Integer, Employee> function1 = Employee::new;
        Employee apply1 = function1.apply(2);
        System.out.println("apply1 = " + apply1);
    }

    // BiFunction中的R apply(T t,U u)
    @Test
    public void test3() {
        BiFunction<Integer, String, Employee> biFunction = (id, name) -> new Employee(id, name);
        Employee e1 = biFunction.apply(1, "张无忌");
        System.out.println("e1 = " + e1);
        System.out.println("----------------------------------------✂----------------------------------------");
        BiFunction<Integer, String, Employee> biFunction1 = Employee::new;
        Employee e2 = biFunction1.apply(2, "赵敏");
        System.out.println("e2 = " + e2);
    }

    // 数组引用
    // Function 中的 R apply(T t)
    @Test
    public void test4() {
        Function<Integer, String[]> function = len -> new String[len];
        String[] apply = function.apply(4);
        System.out.println("apply = " + Arrays.toString(apply));
        System.out.println("----------------------------------------✂----------------------------------------");
        Function<Integer, String[]> function1 = String[]::new;
        String[] apply1 = function1.apply(5);
        System.out.println("apply1 = " + Arrays.toString(apply1));

    }
}
